
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Calendar;

import javax.sql.rowset.WebRowSet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class AdminApp extends JFrame {

	String adminName;
	WebRowSet rs=null;
	DataProcess data=null;
	
	private JPanel contentPane;

	/**
	 * Create the frame.
	 */
	public AdminApp(String admin) {
		
		this.adminName=admin;
		
		try {
			try {
				data=(DataProcess)Naming.lookup("//khangutc/dkh");
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblQunTrVin = new JLabel("Qu\u1EA3n Tr\u1ECB Vi\u00EAn");
		lblQunTrVin.setForeground(Color.RED);
		lblQunTrVin.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblQunTrVin.setBounds(169, 34, 93, 23);
		contentPane.add(lblQunTrVin);
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setBounds(0, 0, 434, 21);
		contentPane.add(menuBar);
		
		JMenu mnOptions = new JMenu("Options");
		menuBar.add(mnOptions);
		
		JMenuItem mntmStudentList = new JMenuItem("Student List");
		mntmStudentList.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent e) {
				StudentUI frm = new StudentUI();
				frm.show();
			}
		});
		mnOptions.add(mntmStudentList);
		
		JMenuItem mntmSubjectList = new JMenuItem("SubJect List");
		mntmSubjectList.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent e) {
				SubjectUI frm = new SubjectUI();
				frm.show();
			}
		});
		mnOptions.add(mntmSubjectList);
		
		JMenuItem mntmClassList = new JMenuItem("Class List");
		mntmClassList.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent e) {
				ClassUI frm = new ClassUI();
				frm.show();				
			}
		});
		mnOptions.add(mntmClassList);
		
		JMenuItem mntmUserList = new JMenuItem("User List");
		mntmUserList.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent e) {
				Student_RegisterUI frm = new Student_RegisterUI();
				frm.show();
			}
		});
		mnOptions.add(mntmUserList);
		
		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				Calendar date = Calendar.getInstance();
				String lastlogin = String.valueOf(date.get(Calendar.DATE)) + "/" + String.valueOf(date.get(Calendar.MONTH)) + 
						 "/" + String.valueOf(date.get(Calendar.YEAR)) + " " + String.valueOf(date.get(Calendar.HOUR)) + 
						 ":" + String.valueOf(date.get(Calendar.MINUTE)) + ":" + String.valueOf(date.get(Calendar.SECOND));
				
				String sql="Update Login set LastLogin='" + lastlogin + "' where UserName='" + adminName + "'";
				try {
					data.ExecUpdateSQL(sql);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
				System.exit(0);
			}
		});
		mnOptions.add(mntmExit);
		
		JLabel lblHello = new JLabel("Hello");
		lblHello.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblHello.setForeground(Color.RED);
		lblHello.setText("Welcome " + getProfile("UserName='" + adminName + "'", "Login"));
		lblHello.setBounds(32, 72, 366, 23);
		contentPane.add(lblHello);
	}
	
	protected String getProfile(String condition, String tableName){
		
		String value="";
		try {
			rs = data.GetRecord(condition, tableName);
			while(rs.next()){
				value=rs.getString(1) + " Last Logged in is " + rs.getString(4); 
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return value;
	}
}
